package org.dydl.controller.system;

import java.util.List;

import org.dydl.common.dto.ResponseMessage;
import org.dydl.model.system.OrderDetail;
import org.dydl.model.system.vo.OrdersVO;
import org.dydl.service.system.OrderDetailService;
import org.dydl.service.system.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单接口
 * @author 张华城
 *
 */
@RestController
@RequestMapping("/api/order")
public class OrderController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private OrderDetailService orderDetailService;
	
//	private static final Logger LOGGER = Logger.getLogger(UploadController.class);      
	/**
	 * 列表
	 * @param page
	 * @param rows
	 * @param orderNo
	 * @return
	 */
	@RequestMapping("/list")
	public ResponseMessage list(int page, int rows, String orderNo){
		
		
		return orderService.list(page, rows, orderNo);
	}
	
	/**
	 * 订单详情列表
	 * @param page
	 * @param rows
	 * @param orderNo
	 * @return
	 */
	@RequestMapping("/detailList")
	public List<OrderDetail> orderDetail(@RequestParam(value = "orderNo", required = false) String orderNo){
		
		return orderService.detailList(orderNo);
	}
	
	
	
 	/**
 	 * 新增/修改
 	 * @param orders
 	 * @return
 	 */
	@RequestMapping("/bothSave")
	public ResponseMessage save(@RequestBody OrdersVO ordersVO){
		
		return orderService.bothSave(ordersVO); 
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public boolean delete(int[] ids){
		if (ids.length != 0) {
			for (int i = 0; i < ids.length; i++) {
				orderService.delete(ids[i]);
			}
			return true;
		} else {
			return false;
		}
		
	}
	
	/**
	 * 审核订单
	 * @param ids
	 */
	@RequestMapping("/toExamine")
	public ResponseMessage examineOrder(@RequestParam(value = "ids", required = false)Integer ids) {
		
		return orderService.examineOrder(ids);
	}
	
	/**
	 * 根据订单号查询订单
	 * @param orderNo
	 * @return
	 */
	@RequestMapping("findByOrderNo")
	public ResponseMessage findByOrderNo(@RequestParam(value = "orderNo", required = false)String orderNo){
		
		
		return orderService.findByOrderNo(orderNo);
	}
	/**
	 * 订单详情列表
	 * @param orderNo
	 * @return
	 */
	@RequestMapping("/selectByOrderNo")
	public ResponseMessage selectByOrderNo(@RequestParam(value = "orderNo", required = false)String orderNo){
		return orderDetailService.findByOrderNo(orderNo);
	}
	
	@RequestMapping("/deleteDetail")
	public ResponseMessage deleteDetail(@RequestBody String orderNo){
		return orderDetailService.delete(orderNo);
	}
	
	/**
	 * 生成流水号
	 * @return
	 */
	@RequestMapping("/serialNo")
	public String orderNo(){
		
		String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		
		char random = chars.charAt((int)(Math.random()*26));
		
		String serialNo = random+""+ System.currentTimeMillis();
		
		return serialNo;
	}
	
}
